package gr.upatras.uniexamtimetable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Room {

	private String name;
	private int roomCode;
	private String roomType;


	public Room() {
		name = "sample room";
		roomCode = 0;
		roomType = "sample type";
	}

	public Room(String newName, int newRoomCode, String newRoomType){
		name = newName;
		roomCode = newRoomCode;
		roomType = newRoomType;
	}

	public String getName() {
		return name;
	}

	public int getRoomCode() {
		return roomCode;
	}

	public String getRoomType() {
		return roomType;
	}

	public void setName(String newName){
		name = newName;	
	}

	public void setRoomCode(int newRoomCode){
		roomCode = newRoomCode;
	}

	public void setRoomType(String newRoomType){
		roomType = newRoomType;
	}

	public void printStatus(){
		System.out.printf("Room name:" + name +
				"%nroomCode:" + roomCode +
				"%nroomType:" + roomType);
		System.out.printf("%n%n");		//+1 empty liny
	}

	public String toString() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(String.format("Name: %s, ", name));
		stringBuilder.append(String.format("Room code: %d, ", roomCode));
		stringBuilder.append(String.format("Room type: %s, ", roomType));
		return stringBuilder.toString();
	}

	/* Exports a list of Room objects to a json file */
	public static void exportToJson(List<Room> rooms, String filename) {

		BufferedWriter writer = null;
		Gson gson = new Gson();

		System.out.printf("Trying to export to %s ...%n", filename);
		
		try {
			writer = new BufferedWriter(new FileWriter(filename));

			/* ugly way to get the type of List<Room> */
			Type collectionType = new TypeToken<List<Room>>(){}.getType();

			/* 
			 * We use toJson(Object src, Type typeOfSrc, Appendable writer)
			 * because Object is a Generic. It was tested that it also works with
			 * toJson(Object src, Appendable writer). However we use the first
			 * because the documentation suggests it.
			 */
			gson.toJson(rooms, collectionType, writer);
		} catch (IOException e) {
			System.err.printf("Sorry could not export to %s.%n", filename);
			e.printStackTrace();			
		} finally {
			try {
				writer.close();		//flushes and closes the writer
			} catch (IOException e) {
				System.err.println("Sorry, couldn't properly close the buffered writer");
				e.printStackTrace();
			} 
		}
		
		System.out.printf("Success: %d obcjects were exported.%n", rooms.size());
	}

	/* Imports a list of Room objects according to a given json file */
	public static List<Room> importFromJson(String filename) {

		BufferedReader reader = null;
		Gson gson = new Gson();
		List<Room> rooms = null;

		System.out.printf("Trying to import from %s ...%n", filename);
				
		try {
			reader = new BufferedReader(new FileReader(filename));

			/* ugly way to get the type of List<Room> */
			Type collectionType = new TypeToken<List<Room>>(){}.getType();
			rooms = gson.fromJson(reader, collectionType);
		} catch (IOException e) {
			System.out.printf("Sorry, couldn't import from %s.%n", filename);
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				System.err.println("Sorry, couldn't close the buffered reader.");
				e.printStackTrace();
			}
		}
		
		System.out.printf("Success: %d obcjects were imported.%n", rooms.size());
		return rooms;		
	}

}
